home *** CD-ROM | disk | FTP | other *** search
- Path: news.primenet.com!not-for-mail
- From: gbe@primenet.com (Gary Edstrom)
- Newsgroups: comp.lang.c++
- Subject: Re: What is Object Oriented Programming (OOP)?
- Date: 4 Feb 1996 08:19:01 -0700
- Organization: Sequoia Software
- Sender: root@primenet.com
- Message-ID: <3114c73f.290832739@news.primenet.com>
- References: <4f1nj6$4cie@news-s01.ny.us.ibm.net>
- X-Posted-By: ip037.lax.primenet.com
- X-Newsreader: Forte Agent .99c/16.141
-
- >sansw@ibm.net (SAN'Z) wrote:
-
- >Hello to all internet user,
-
- >I'm just starting to learn C++ and I want to know about OOP.
- >Can you explain to me about that? And I really want to know and study it.
-
- A good real world analogy of an object would be your car. A car consists of a
- number of input functions such as the accelerator, brake, ignition key, etc.
- It also consists of a number of output functions such as the dashboard display
- of speed, distance, engine temperature, fuel, etc. You, as the user of this
- object, do not need to really have much knowledge of how it handles these
- functions internally, you just know that to make the car go faster, you press
- on the accelerator.
-
- Objects in the programming are similar. In a C++ object, we hide the details
- about how data is stored inside the object, as well as exactly how the various
- functions are performed internally. An object consists of both data, and a
- series of functions that can manipulate that data. Normally, you only make the
- functions visible to the outside world and hide the data.
-
- One common C++ data type is the "complex" type. The knowledge of how to
- manipulate complex numbers is not something that has been hard coded into the
- compiler. Instead, an object called "complex" has been defined that performs
- all of the functions that we commonly associate with complex numbers, such as
- addition, subtraction, multiplication, etc. To the programmer, using a complex
- number is a simple process:
-
- complex x(3,7);
- complex y(4,5);
- complex z = x * y;
-
- Hidden behind the scenes, however, exist header and code files that implement
- the "complex" data type. A simplified definition of the "complex" type that
- would handle the above sample would be as follows:
-
- class complex
- {
- private:
- float real;
- float imag;
-
- public:
- complex() { }
- complex(float a, float b) { real = a; imag = b; }
-
- const complex & operator = (const complex & source)
- {
- real = source.real;
- imag = source.imag;
- }
-
- complex operator * (const complex & source)
- {
- complex temp = *this;
- temp.real = real * source.real - imag * source.imag;
- temp.imag = real * source.imag + imag * source.real;
- return temp;
- }
- };
-
- Of course, the real "complex" class implements a lot more functions than I have
- shown here. All of the familiar C++ operators such as +=, -=, *=, etc need to
- be defined if we are going to create a class that has all of the operations
- that we normally associate with simple integers.
-
- Not all user defined data types would necessarily have to overload all of the
- operators. For a particular data type, some of the operators may simply make
- no sense at all. Therefore, they are left out.
-
- Once again: The purpose of an object is to group a number of related items
- together and to hide the details of exactly how those items are manipulated.
-
- I hope that helps a little.
-
- --
- Gary Edstrom <gbe@primenet.com> | Sequoia Software
- PO Box 9573 | Programming & Technical Services
- Glendale CA 91226-0573 | PGP Key ID: 0x1A0D44BD
- PGP Fingerprint: 72 AA 4F 73 05 53 89 C6 8A EE F4 EE D1 C0 13 8D
-